<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>integration_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_fs_integration_test.js.html">integration_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.fs.integrationTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.fs.integrationTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line19"></a>goog.require(&#39;goog.async.DeferredList&#39;);
<a name="line20"></a>goog.require(&#39;goog.events&#39;);
<a name="line21"></a>goog.require(&#39;goog.fs&#39;);
<a name="line22"></a>goog.require(&#39;goog.fs.DirectoryEntry&#39;);
<a name="line23"></a>goog.require(&#39;goog.fs.Error&#39;);
<a name="line24"></a>goog.require(&#39;goog.fs.FileSaver&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.fs&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line29"></a>
<a name="line30"></a>var TEST_DIR = &#39;goog-fs-test-dir&#39;;
<a name="line31"></a>
<a name="line32"></a>var deferredFs = goog.testing.fs.getTemporary();
<a name="line33"></a>var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
<a name="line34"></a>
<a name="line35"></a>function setUpPage() {
<a name="line36"></a>  goog.testing.fs.install(new goog.testing.PropertyReplacer());
<a name="line37"></a>}
<a name="line38"></a>
<a name="line39"></a>function tearDown() {
<a name="line40"></a>  loadTestDir().
<a name="line41"></a>      addCallback(function(dir) { return dir.removeRecursively(); }).
<a name="line42"></a>      addBoth(continueTesting);
<a name="line43"></a>  asyncTestCase.waitForAsync(&#39;removing filesystem&#39;);
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>function testWriteFile() {
<a name="line47"></a>  loadFile(&#39;test&#39;, goog.fs.DirectoryEntry.Behavior.CREATE).
<a name="line48"></a>      addCallback(goog.partial(writeToFile, &#39;test content&#39;)).
<a name="line49"></a>      addCallback(goog.partial(checkFileContent, &#39;test content&#39;)).
<a name="line50"></a>      addBoth(continueTesting);
<a name="line51"></a>  asyncTestCase.waitForAsync(&#39;testWriteFile&#39;);
<a name="line52"></a>}
<a name="line53"></a>
<a name="line54"></a>function testRemoveFile() {
<a name="line55"></a>  loadFile(&#39;test&#39;, goog.fs.DirectoryEntry.Behavior.CREATE).
<a name="line56"></a>      addCallback(goog.partial(writeToFile, &#39;test content&#39;)).
<a name="line57"></a>      addCallback(function(fileEntry) { return fileEntry.remove(); }).
<a name="line58"></a>      addCallback(goog.partial(checkFileRemoved, &#39;test&#39;)).
<a name="line59"></a>      addBoth(continueTesting);
<a name="line60"></a>  asyncTestCase.waitForAsync(&#39;testRemoveFile&#39;);
<a name="line61"></a>}
<a name="line62"></a>
<a name="line63"></a>function testMoveFile() {
<a name="line64"></a>  var deferredSubdir = loadDirectory(
<a name="line65"></a>      &#39;subdir&#39;, goog.fs.DirectoryEntry.Behavior.CREATE);
<a name="line66"></a>  var deferredWrittenFile =
<a name="line67"></a>      loadFile(&#39;test&#39;, goog.fs.DirectoryEntry.Behavior.CREATE).
<a name="line68"></a>      addCallback(goog.partial(writeToFile, &#39;test content&#39;));
<a name="line69"></a>  goog.async.DeferredList.gatherResults([deferredSubdir, deferredWrittenFile]).
<a name="line70"></a>      addCallback(splitArgs(function(dir, fileEntry) {
<a name="line71"></a>        return fileEntry.moveTo(dir);
<a name="line72"></a>      })).
<a name="line73"></a>      addCallback(goog.partial(checkFileContent, &#39;test content&#39;)).
<a name="line74"></a>      addCallback(goog.partial(checkFileRemoved, &#39;test&#39;)).
<a name="line75"></a>      addBoth(continueTesting);
<a name="line76"></a>  asyncTestCase.waitForAsync(&#39;testMoveFile&#39;);
<a name="line77"></a>}
<a name="line78"></a>
<a name="line79"></a>function testCopyFile() {
<a name="line80"></a>  var deferredFile = loadFile(&#39;test&#39;, goog.fs.DirectoryEntry.Behavior.CREATE);
<a name="line81"></a>  var deferredSubdir = loadDirectory(
<a name="line82"></a>      &#39;subdir&#39;, goog.fs.DirectoryEntry.Behavior.CREATE);
<a name="line83"></a>  var deferredWrittenFile = deferredFile.branch().
<a name="line84"></a>      addCallback(goog.partial(writeToFile, &#39;test content&#39;));
<a name="line85"></a>  goog.async.DeferredList.gatherResults([deferredSubdir, deferredWrittenFile]).
<a name="line86"></a>      addCallback(splitArgs(function(dir, fileEntry) {
<a name="line87"></a>        return fileEntry.copyTo(dir);
<a name="line88"></a>      })).
<a name="line89"></a>      addCallback(goog.partial(checkFileContent, &#39;test content&#39;)).
<a name="line90"></a>      awaitDeferred(deferredFile).
<a name="line91"></a>      addCallback(goog.partial(checkFileContent, &#39;test content&#39;)).
<a name="line92"></a>      addBoth(continueTesting);
<a name="line93"></a>  asyncTestCase.waitForAsync(&#39;testCopyFile&#39;);
<a name="line94"></a>}
<a name="line95"></a>
<a name="line96"></a>function testAbortWrite() {
<a name="line97"></a>  var deferredFile = loadFile(&#39;test&#39;, goog.fs.DirectoryEntry.Behavior.CREATE);
<a name="line98"></a>  deferredFile.branch().
<a name="line99"></a>      addCallback(goog.partial(startWrite, &#39;test content&#39;)).
<a name="line100"></a>      addCallback(function(writer) { writer.abort(); }).
<a name="line101"></a>      addCallback(
<a name="line102"></a>          goog.partial(waitForEvent, goog.fs.FileSaver.EventType.ABORT)).
<a name="line103"></a>      awaitDeferred(deferredFile).
<a name="line104"></a>      addCallback(goog.partial(checkFileContent, &#39;&#39;)).
<a name="line105"></a>      addBoth(continueTesting);
<a name="line106"></a>  asyncTestCase.waitForAsync(&#39;testAbortWrite&#39;);
<a name="line107"></a>}
<a name="line108"></a>
<a name="line109"></a>function testSeek() {
<a name="line110"></a>  var deferredFile = loadFile(&#39;test&#39;, goog.fs.DirectoryEntry.Behavior.CREATE);
<a name="line111"></a>  deferredFile.branch().
<a name="line112"></a>      addCallback(goog.partial(writeToFile, &#39;test content&#39;)).
<a name="line113"></a>      addCallback(function(fileEntry) { return fileEntry.createWriter(); }).
<a name="line114"></a>      addCallback(
<a name="line115"></a>          goog.partial(checkReadyState, goog.fs.FileSaver.ReadyState.INIT)).
<a name="line116"></a>      addCallback(function(writer) {
<a name="line117"></a>        writer.seek(5);
<a name="line118"></a>        writer.write(goog.fs.getBlob(&#39;stuff and things&#39;));
<a name="line119"></a>      }).
<a name="line120"></a>      addCallback(
<a name="line121"></a>          goog.partial(checkReadyState, goog.fs.FileSaver.ReadyState.WRITING)).
<a name="line122"></a>      addCallback(
<a name="line123"></a>          goog.partial(waitForEvent, goog.fs.FileSaver.EventType.WRITE)).
<a name="line124"></a>      awaitDeferred(deferredFile).
<a name="line125"></a>      addCallback(goog.partial(checkFileContent, &#39;test stuff and things&#39;)).
<a name="line126"></a>      addBoth(continueTesting);
<a name="line127"></a>  asyncTestCase.waitForAsync(&#39;testSeek&#39;);
<a name="line128"></a>}
<a name="line129"></a>
<a name="line130"></a>function testTruncate() {
<a name="line131"></a>  var deferredFile = loadFile(&#39;test&#39;, goog.fs.DirectoryEntry.Behavior.CREATE);
<a name="line132"></a>  deferredFile.branch().
<a name="line133"></a>      addCallback(goog.partial(writeToFile, &#39;test content&#39;)).
<a name="line134"></a>      addCallback(function(fileEntry) { return fileEntry.createWriter(); }).
<a name="line135"></a>      addCallback(
<a name="line136"></a>          goog.partial(checkReadyState, goog.fs.FileSaver.ReadyState.INIT)).
<a name="line137"></a>      addCallback(function(writer) { writer.truncate(4); }).
<a name="line138"></a>      addCallback(
<a name="line139"></a>          goog.partial(checkReadyState, goog.fs.FileSaver.ReadyState.WRITING)).
<a name="line140"></a>      addCallback(
<a name="line141"></a>          goog.partial(waitForEvent, goog.fs.FileSaver.EventType.WRITE)).
<a name="line142"></a>      awaitDeferred(deferredFile).
<a name="line143"></a>      addCallback(goog.partial(checkFileContent, &#39;test&#39;)).
<a name="line144"></a>      addBoth(continueTesting);
<a name="line145"></a>  asyncTestCase.waitForAsync(&#39;testTruncate&#39;);
<a name="line146"></a>}
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>function continueTesting(result) {
<a name="line150"></a>  asyncTestCase.continueTesting();
<a name="line151"></a>  if (result instanceof Error) {
<a name="line152"></a>    throw result;
<a name="line153"></a>  }
<a name="line154"></a>}
<a name="line155"></a>
<a name="line156"></a>function loadTestDir() {
<a name="line157"></a>  return deferredFs.branch().addCallback(function(fs) {
<a name="line158"></a>    return fs.getRoot().getDirectory(
<a name="line159"></a>        TEST_DIR, goog.fs.DirectoryEntry.Behavior.CREATE);
<a name="line160"></a>  });
<a name="line161"></a>}
<a name="line162"></a>
<a name="line163"></a>function loadFile(filename, behavior) {
<a name="line164"></a>  return loadTestDir().addCallback(function(dir) {
<a name="line165"></a>    return dir.getFile(filename, behavior);
<a name="line166"></a>  });
<a name="line167"></a>}
<a name="line168"></a>
<a name="line169"></a>function loadDirectory(filename, behavior) {
<a name="line170"></a>  return loadTestDir().addCallback(function(dir) {
<a name="line171"></a>    return dir.getDirectory(filename, behavior);
<a name="line172"></a>  });
<a name="line173"></a>}
<a name="line174"></a>
<a name="line175"></a>function startWrite(content, fileEntry) {
<a name="line176"></a>  return fileEntry.createWriter().
<a name="line177"></a>      addCallback(
<a name="line178"></a>          goog.partial(checkReadyState, goog.fs.FileSaver.ReadyState.INIT)).
<a name="line179"></a>      addCallback(function(writer) {
<a name="line180"></a>        writer.write(goog.fs.getBlob(content));
<a name="line181"></a>        return writer;
<a name="line182"></a>      }).
<a name="line183"></a>      addCallback(
<a name="line184"></a>          goog.partial(checkReadyState, goog.fs.FileSaver.ReadyState.WRITING));
<a name="line185"></a>}
<a name="line186"></a>
<a name="line187"></a>function waitForEvent(type, target) {
<a name="line188"></a>  var d = new goog.async.Deferred();
<a name="line189"></a>  goog.events.listenOnce(target, type, d.callback, false, d);
<a name="line190"></a>  return d;
<a name="line191"></a>}
<a name="line192"></a>
<a name="line193"></a>function writeToFile(content, fileEntry) {
<a name="line194"></a>  return startWrite(content, fileEntry).
<a name="line195"></a>      addCallback(
<a name="line196"></a>          goog.partial(waitForEvent, goog.fs.FileSaver.EventType.WRITE)).
<a name="line197"></a>      addCallback(function() { return fileEntry; });
<a name="line198"></a>}
<a name="line199"></a>
<a name="line200"></a>function checkFileContent(content, fileEntry) {
<a name="line201"></a>  return fileEntry.file().
<a name="line202"></a>      addCallback(function(blob) { return goog.fs.blobToString(blob); }).
<a name="line203"></a>      addCallback(goog.partial(assertEquals, content));
<a name="line204"></a>}
<a name="line205"></a>
<a name="line206"></a>function checkFileRemoved(filename) {
<a name="line207"></a>  return loadFile(filename).
<a name="line208"></a>      addCallback(goog.partial(fail, &#39;expected file to be removed&#39;)).
<a name="line209"></a>      addErrback(function(err) {
<a name="line210"></a>        assertEquals(err.code, goog.fs.Error.ErrorCode.NOT_FOUND);
<a name="line211"></a>        return true; // Go back to callback path
<a name="line212"></a>      });
<a name="line213"></a>}
<a name="line214"></a>
<a name="line215"></a>function checkReadyState(expectedState, writer) {
<a name="line216"></a>  assertEquals(expectedState, writer.getReadyState());
<a name="line217"></a>}
<a name="line218"></a>
<a name="line219"></a>function splitArgs(fn) {
<a name="line220"></a>  return function(args) { return fn(args[0], args[1]); };
<a name="line221"></a>}
</pre>


</body>
</html>
